package com.yanqu.road.server.manger.activity.signindraw;

import com.yanqu.road.dao.impl.activity.signindraw.SignInDrawConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.signindraw.SignInDrawConfig;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.signindraw.SignInDrawModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;

public class SignInDrawActivityMgr extends TempMgr {

    private static SignInDrawConfig config;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }


    public static void reloadActivityData() {

        getLogger().info("reload SignInDraw Activity start");

        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SignInDraw.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no SignInDraw Activity in show time");
            config = null;

        } else {

            ActivityInfo activityInfo = openActivityInfoList.get(0);
            SignInDrawConfig tempConfig = new SignInDrawConfig(activityInfo);
            int tmpActivityId = activityInfo.getActivityId();

            SignInDrawConfigDaoImpl configDao = new SignInDrawConfigDaoImpl();
            tempConfig.setDrawInfoList(configDao.getSignInDrawDrawInfoList(tmpActivityId));

            //初始化配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_SUPPLEMENTARY_SIGNATURE_CONSUME.getName())) {
                tempConfig.setSIGNATURE_CONSUME(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_SUPPLEMENTARY_SIGNATURE_CONSUME.getName()).getValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_SIGNIN_REWARD.getName())) {
                tempConfig.setSIGNIN_REWARD(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_SIGNIN_REWARD.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_MAKE_WISH_CONSUME.getName())) {
                tempConfig.setMAKE_WISH_CONSUME(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_MAKE_WISH_CONSUME.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_ANNOUNCEMENT_QUANTITY.getName())) {
                tempConfig.setANNOUNCEMENT_QUANTITY(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_ANNOUNCEMENT_QUANTITY.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_SHARING_REWARD.getName())) {
                tempConfig.setSHARING_REWARD(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_SHARING_REWARD.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_ACTIVE_LIMIT.getName())) {
                tempConfig.setACTIVE_LIMIT(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_ACTIVE_LIMIT.getName()).getIntValue());
            }

            if (activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_LOTTERY_VIP_LIMIT.getName())) {
                tempConfig.setLOTTERY_VIP_LIMIT(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_LOTTERY_VIP_LIMIT.getName()).getValue());
            }
            tempConfig.setPATRONS_ID(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_PATRONS_ID.getName()).getIntValue());
            tempConfig.setPATRONS_TRANSFORM_ITEM(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_PATRONS_TRANSFORM_ITEM.getName()).getValue());
            tempConfig.setGOODS_AUTO_CONSUME_TITLE(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_GOODS_AUTO_CONSUME_TITLE.getName()).getValue());
            tempConfig.setGOODS_AUTO_CONSUME_CONTENT(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_GOODS_AUTO_CONSUME_CONTENT.getName()).getValue());

            if(activityInfo.getChildType() == 2){
                if(activityConfigMap.containsKey(eActivityConfigType.ANNIVERSARY_ONLINE_REWARD.getName())){
                    List<Integer> list = StringUtils.stringToIntegerList(activityConfigMap.get(eActivityConfigType.ANNIVERSARY_ONLINE_REWARD.getName()).getValue(), "\\|");
                    tempConfig.setOnlineTime(list.get(0));
                    tempConfig.setOnlineReward(list.get(1));
                }else {
                    getLogger().info("activityId {} need ANNIVERSARY_ONLINE_REWARD", activityInfo.getActivityId());
                }
            }
            config = tempConfig;
        }

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            SignInDrawModule module = player.getModule(SignInDrawModule.class);
            module.syncConfig();
            module.initUserData();
            module.syncUserData();
        }

        getLogger().info("reload SignInDraw Activity finish");
    }


    public static SignInDrawConfig getConfig() {
        return config;
    }

    public static ActivityInfo getActivityInfo(){
        if(config == null){
            return null;
        }
        return config.getActivityInfo();
    }

    public static int getActivityId(){
        ActivityInfo activityInfo = getActivityInfo();
        if(activityInfo == null){
            return 0;
        }
        return activityInfo.getActivityId();
    }
}
